//
//  iCCustomPageControl.m
//  MarryBackEnd
//
//  Created by mac on 2021/1/6.
//  Copyright © 2021 WPP. All rights reserved.
//

#import "iCCustomPageControl.h"
#define kNormalColor rgba(205, 195, 180, 1)
#define kSelectedColor rgba(141, 112, 70, 1)
@interface iCCustomPageControl ()
@property(nonatomic,strong)NSMutableArray<UIView *> *pgArray;
@end

@implementation iCCustomPageControl
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        
    }
    return self;
}
- (void)dealloc
{
    NSLog(@"CustomPageControl ---dealloc");
}
#pragma mark - setter
- (void)setNumberOfPages:(NSInteger)numberOfPages
{
    _numberOfPages = numberOfPages;
    if (numberOfPages <= 1) {
        return;
    }
    [self.pgArray enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.pgArray removeAllObjects];
    CGFloat itemWidth = 6*ADAPTER_WIDTH,itemHeight = 6*ADAPTER_WIDTH,marginX = 5*ADAPTER_WIDTH;
    CGFloat totalWidth = (numberOfPages * (itemWidth + marginX)) - marginX;
    CGFloat orinX = (self.width - totalWidth) * 0.5;
    for (NSInteger index = 0; index < numberOfPages; index ++) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(orinX + index * (itemWidth + marginX), (self.height - itemHeight) * 0.5, itemWidth, itemHeight)];
        [self addSubview:view];
        view.backgroundColor = kNormalColor;
        view.layer.cornerRadius = itemHeight * 0.5;
        view.layer.masksToBounds = YES;
        [self.pgArray addObject:view];
        
        if (index == 0) {
            view.backgroundColor = kSelectedColor;
        }
    }
}
- (void)setCurrentPage:(NSInteger)currentPage
{
    _currentPage = currentPage;
    for (NSInteger index = 0; index < self.pgArray.count; index ++) {
        UIView *view = self.pgArray[index];
        if (index == currentPage) {
            view.backgroundColor = kSelectedColor;
        } else {
            view.backgroundColor = kNormalColor;
        }
    }
}
#pragma mark- lazy懒加载区域
- (NSMutableArray<UIView *> *)pgArray
{
    if (!_pgArray) {
        _pgArray = [NSMutableArray array];
    }
    return _pgArray;
}
@end
